home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / URL Helper II / Source / URLHLibrary.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-14  |  4.7 KB  |  223 lines  |  [TEXT/MMCC]

  1. /***
  2.  * URLHLibrary.c
  3.  *
  4.  *  Library of interface routines to make life simpler when trying to
  5.  *  access the URLH component.
  6.  *
  7.  *  This library assumes that global data is an ok thing to use.  If
  8.  *  you don't want to use it, then hack this puppy apart and 
  9.  *  cut/paste what parts you need.
  10.  *
  11.  *    HEY -- This file assumes that a parseref points to valid memory.
  12.  *  This is certianly true if the current implementation, and will be
  13.  *  for at least a while.  Things might well break if it isn't...  Perhaps
  14.  *  I should fix that before this goes production!
  15.  ***/
  16.  
  17. #include "URLHelperComponent.h"
  18. #include "URLHLibrary.h"
  19.  
  20. URLHelperComponent            urlInstance = 0;
  21.  
  22. typedef struct {
  23.     long        check;        // Hold a hash number so we can id this guy
  24.     Str255        url;        // Name of failed url
  25. } bumParseRef, *bumParseRefPtr;
  26.  
  27. #define CHECK_ID    0x2a45f8ab
  28.  
  29. /**
  30.  * URHLibOpen
  31.  *
  32.  *  Open up the component -- return an error if we can't
  33.  *
  34.  **/
  35. OSErr URLHLibOpen (void)
  36. {
  37.     if (urlInstance == 0)
  38.         urlInstance = OpenDefaultComponent (URLHelperComponentType, 0L);
  39.  
  40.     if (urlInstance == 0)
  41.         return -1;
  42.     
  43.     return noErr;
  44. }
  45.  
  46. /**
  47.  * URLHLibClose
  48.  *
  49.  *   Close the component, if it is open!
  50.  *
  51.  **/
  52. void URLHLibClose (void)
  53. {
  54.     if (urlInstance)
  55.         CloseComponent (urlInstance);
  56.     urlInstance = 0;
  57. }
  58.  
  59. /**
  60.  * URLHLibUse
  61.  *
  62.  *  Used if you want to use a particular instance of the component.
  63.  *
  64.  **/
  65. void URLHLibUse (URLHelperComponent instance)
  66. {
  67.     URLHLibClose ();
  68.     urlInstance = instance;
  69. }
  70.  
  71. /**
  72.  * URLHLibGetFirstMirror
  73.  *
  74.  *  Return the first mirror we find for the given url.  Return the url
  75.  *  if there is no URLHelper component on the system or if we can't
  76.  *  find the damm url in our mirror list.
  77.  *
  78.  *  It is ok for the baseURL and mirrorURL to point to the same memory.
  79.  *  This routine should always return noErr...
  80.  *
  81.  **/
  82. void URLHLibGetFirstMirror (StringPtr baseURL, StringPtr mirrorURL)
  83. {
  84.     OSErr            theErr;
  85.     URLHParseRef    parseRef;
  86.     Boolean            disposeParseState;
  87.  
  88.     /**
  89.      ** First, check to see if we can open up the url component...
  90.      **/
  91.  
  92.     theErr = URLHLibOpen ();
  93.  
  94.     /**
  95.      ** Now, parse the darn thing
  96.      **/
  97.     
  98.     if (theErr == noErr)
  99.         theErr = URLHNewParseState (urlInstance, baseURL, &parseRef);    
  100.     disposeParseState = theErr == noErr;
  101.     
  102.     /**
  103.      ** And get back the first mirror we can
  104.      **/
  105.     
  106.     if (theErr == noErr)
  107.         theErr = URLHGetMirrorRep (urlInstance, parseRef, mirrorURL, 1);
  108.  
  109.     if (disposeParseState)
  110.         URLHDisposeParseState (urlInstance, parseRef);
  111.  
  112.     /** 
  113.      ** If there has been an error, then copy over the old url into the new url...
  114.      **/
  115.  
  116.     if (theErr != noErr
  117.         && baseURL != mirrorURL)
  118.             BlockMove (baseURL, mirrorURL, baseURL[0]+1);
  119.  
  120.  
  121.     return;
  122. }
  123.  
  124. /**
  125.  ** The next three routines allow one to access all the mirrors in the list, one by one.
  126.  ** The caller must keep track of a parse state variable.  These routines are designed to
  127.  ** work weather or not the component is present on the system.
  128.  **/
  129.  
  130. /**
  131.  * URLHLibNewParseState
  132.  *
  133.  *  This routine will create a new parse state.  Pass in the baseURL and get back the
  134.  *  parse state.
  135.  *
  136.  **/
  137. URLHParseRef URLHLibNewParseState (StringPtr baseURL)
  138. {
  139.     OSErr            theErr;
  140.     URLHParseRef    parseRef;
  141.     bumParseRefPtr    bummer;
  142.  
  143.     theErr = URLHLibOpen ();
  144.     
  145.     if (theErr == noErr)
  146.         theErr = URLHNewParseState (urlInstance, baseURL, &parseRef);    
  147.  
  148.     if (theErr == noErr)
  149.         return parseRef;
  150.         
  151.     /**
  152.      ** Bummer.  We didn't do it.  Create our own struct so we can communicate this
  153.      ** info back to the next set of routines...
  154.      **/
  155.  
  156.     bummer = (bumParseRefPtr) NewPtrClear (sizeof (bumParseRef));
  157.     bummer->check = CHECK_ID;
  158.     BlockMove (baseURL, bummer->url, baseURL[0]+1);
  159.  
  160.     return (URLHParseRef) bummer;
  161. }
  162.  
  163. /**
  164.  * URLHLibGetIndexedMirror
  165.  *
  166.  *  Return (from a parse state gotten from LibNewParseState) the index'edth mirror.  OSErr
  167.  *  will be -1 when we have looped through all the mirrors.
  168.  *
  169.  **/
  170. Boolean URLHLibGetIndexedMirror (URLHParseRef parseRef, short index, StringPtr mirrorURL)
  171. {
  172.     OSErr            theErr;
  173.     bumParseRefPtr    bummer;
  174.     
  175.     mirrorURL[0] = 0;
  176.  
  177.     /**
  178.      ** Check to see if we have a bum parse ref pointer...
  179.      **/
  180.  
  181.     bummer = (bumParseRefPtr) parseRef;
  182.     if (bummer->check == CHECK_ID) {
  183.         if (index != 1)
  184.             return false;
  185.         BlockMove (bummer->url, mirrorURL, bummer->url[0]+1);
  186.         return true;
  187.     }
  188.     
  189.     /**
  190.      ** Ok -- normal url.  deal with it!
  191.      **/
  192.     
  193.     theErr = URLHGetMirrorRep (urlInstance, parseRef, mirrorURL, index);
  194.     if (theErr != noErr)
  195.         return false;
  196.     
  197.     return true;
  198. }
  199.  
  200. /**
  201.  * URLHLibDisposeParseState
  202.  *
  203.  *  This guy will get rid of an old parse ref
  204.  *
  205.  **/
  206. void URLHLibDisposeParseState (URLHParseRef parseRef)
  207. {
  208.     bumParseRefPtr        bummer;
  209.     
  210.     /**
  211.      ** Make sure it isn't one of our bummer guys...
  212.      **/
  213.     
  214.     bummer = (bumParseRefPtr) parseRef;
  215.     if (bummer->check == CHECK_ID) {
  216.         DisposPtr ((Ptr) bummer);
  217.         return;
  218.     }
  219.  
  220.     URLHDisposeParseState (urlInstance, parseRef);
  221.     return;
  222.  
  223. }